/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.excesys.exsecs.access.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.access.dto.AccessSupplierDTO;
import com.excesys.exsecs.access.entity.AccessSupplier;
import com.excesys.exsecs.access.service.AccessSupplierService;
import com.excesys.exsecs.access.util.BeanMapUtil;
import com.excesys.exsecs.access.vo.AccessSupplierExcelVO;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.RequestExcel;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 供应商信息表
 *
 * @author lxc
 * @date 2024-05-06 17:44:39
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/accessSupplier")
@Tag(name = "供应商管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AccessSupplierController {

	private final AccessSupplierService accessSupplierService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param AccessSupplier 供应商
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('access_accessSupplier_get')")
	public R getAccessSupplierPage(Page page, AccessSupplierDTO AccessSupplier) {
		// return R.ok(AccessSupplierService.page(page,
		// Wrappers.query(AccessSupplier)));
		return R.ok(accessSupplierService.getSupplierByPage(page, AccessSupplier));
	}

	@Operation(summary = "根据条件查询不分页", description = "条件查询")
	@PostMapping("/getSupplierBy")
	@PreAuthorize("@pms.hasPermission('access_accessSupplier_get')")
	public R getSupplierBy(@RequestBody AccessSupplierDTO supplierDTO) {
		return R.ok(accessSupplierService.list(accessSupplierService.buildQueryWrapper(supplierDTO)));
	}

	/**
	 * 通过id查询供应商
	 * @param supplierId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{supplierId}")
	@PreAuthorize("@pms.hasPermission('access_accessSupplier_get')")
	public R getById(@PathVariable("supplierId") String supplierId) {
		return R.ok(accessSupplierService.getById(supplierId));
	}

	/**
	 * 新增供应商
	 * @param AccessSupplier 供应商
	 * @return R
	 */
	@Operation(summary = "新增供应商", description = "新增供应商")
	@SysLog("新增供应商")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('access_accessSupplier_add')")
	public R save(@RequestBody AccessSupplier AccessSupplier) {
		return R.ok(accessSupplierService.save(AccessSupplier));
	}

	/**
	 * 修改供应商
	 * @param AccessSupplier 供应商
	 * @return R
	 */
	@Operation(summary = "修改供应商", description = "修改供应商")
	@SysLog("修改供应商")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('access_accessSupplier_edit')")
	public R updateById(@RequestBody AccessSupplier AccessSupplier) {
		return R.ok(accessSupplierService.updateById(AccessSupplier));
	}

	/**
	 * 通过id删除供应商
	 * @param ids
	 * @return R
	 */
	@Operation(summary = "通过id删除供应商", description = "通过id删除供应商")
	@SysLog("通过id删除供应商")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('access_accessSupplier_del')")
	public R removeById(@RequestBody String[] supplierIds) {
		return R.ok(accessSupplierService.removeBatchByIds(CollUtil.toList(supplierIds)));
	}

	@Operation(summary = "供应商编码生成器", description = "生成供应商编码")
	@GetMapping("/getNumber")
	// @PreAuthorize("@pms.hasPermission('bm_Accesscloumn_get')" )
	public R getNumber() throws ParseException {
		return R.ok(accessSupplierService.getNum());
	}

	@Operation(summary = "根据条件查询不分页返回listMap", description = "条件查询")
	@GetMapping("/listMapByWhere")
	@PreAuthorize("@pms.hasPermission('access_accessSupplier_get')")
	public R<List<Map<String, Object>>> listByWhere(@RequestParam Map param) throws Exception {
		AccessSupplier AccessSupplier = BeanMapUtil.mapToBean(param, AccessSupplier.class);
		List<AccessSupplier> dataList = accessSupplierService.list(Wrappers.query(AccessSupplier));
		List<Map<String, Object>> mapList = BeanMapUtil.objectListToListMap(dataList);
		return R.ok(mapList, "根据条件查询不分页返回listMap");
	}

	@Operation(summary = "导出所有数据", description = "导出所有数据")
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('access_accessSupplier_export')")
	public List<AccessSupplierExcelVO> export(AccessSupplierDTO bamAssetsType) {
		return accessSupplierService.listAccessSupplier(bamAssetsType);
	}

	/**
	 * 导入资产类型
	 * @param excelVOList 资产类型列表
	 * @param bindingResult 错误信息列表
	 * @return ok fail
	 */
	@PostMapping("/import")
	@PreAuthorize("@pms.hasPermission('access_accessSupplier_import')")
	public R importRole(@RequestExcel List<AccessSupplierExcelVO> excelVOList, BindingResult bindingResult) {
		return accessSupplierService.importAccessSupplier(excelVOList, bindingResult);
	}

}
